package ru.rs.interfaces.impl;

import ru.rs.interfaces.Game;
import ru.rs.interfaces.Graphics;
import ru.rs.interfaces.Input;
import ru.rs.objects.RenderView;
import ru.rs.objects.Screen;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class AndroidGame extends Activity implements Game {

	private Graphics graphics;

	private Screen screen;

	private RenderView view;

	private Input input;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		boolean isLand = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

		int w = isLand ? 480 : 320, h = isLand ? 320 : 480;
		Bitmap frame = Bitmap.createBitmap(w, h, Config.RGB_565);

		float sX = (float) w
				/ getWindowManager().getDefaultDisplay().getWidth();
		float sY = (float) h
				/ getWindowManager().getDefaultDisplay().getHeight();

		graphics = new GraphicsImpl(getAssets(), frame);
		view = new RenderView(this, frame);
		input = new InputImpl(view, sX, sY);
		screen = getStartScreen();
		setContentView(view);
	}

	public Graphics getGraphics() {

		// TODO Auto-generated method stub
		return graphics;
	}

	public Screen getCurrentScreen() {

		// TODO Auto-generated method stub
		return screen;
	}

	public Screen getStartScreen() {

		// TODO Auto-generated method stub
		return null;
	}

	public void setScreen(Screen screen) {

		this.screen.pause();
		this.screen.dispose();
		screen.resume();
		screen.update(0);

		this.screen = screen;
	}

	@Override
	public void onPause() {

		super.onPause();
		view.pause();
		screen.pause();
		if (isFinishing()) {
			screen.dispose();
		}
	}

	@Override
	public void onResume() {

		super.onResume();
		screen.resume();
		view.resume();
	}

	public Input getInput() {

		// TODO Auto-generated method stub
		return input;
	}
}
